package sy.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;

import sy.dao.SysInformationDaoI;
import sy.dao.SysProjectInformationMappingDaoI;
import sy.model.PerAddress;
import sy.model.SysInformation;
import sy.model.SysProject;
import sy.model.SysProjectInformationMapping;
import sy.service.SysInformationServiceI;
import sy.service.SysProjectInformationMappingServiceI;
import sy.service.SysProjectServiceI;

@Service("sysInformationService")
public class SysInformationServiceImpl implements SysInformationServiceI {
	
	private static final Logger logger = Logger.getLogger(SysInformation.class);
	@Autowired
	private SysInformationDaoI sysInformationDao;
	@Autowired
	private SysProjectInformationMappingServiceI sysProjectInformationMappingService;
	@Autowired
	private SysProjectServiceI sysProjectService;

	
	
	private void writeIntoProjectInformationMapping(SysInformation sysInformation,String[] projectIds){
		List<SysProjectInformationMapping> projInfoList = new ArrayList<SysProjectInformationMapping>();
		String uuid = "";
		/**
		 * 构造项目信息中间表，写入数据库
		 * */
		for(int i = 0; i<projectIds.length; i++){
			//构造中间表
			SysProjectInformationMapping projInfo = new SysProjectInformationMapping();
			uuid = UUID.randomUUID().toString();
			projInfo.setId(uuid);
			//将公告信息写入中间表
			projInfo.setSysInformation(sysInformation);
			//将项目信息写入中间表
			SysProject project = sysProjectService.getSysProjectById(projectIds[i]);
			projInfo.setSysProject(project);
			//叠加
			projInfoList.add(projInfo);
		} 
		sysProjectInformationMappingService.saveSysProjectInformationMappings(projInfoList);
	};
	
	@Override
	public Serializable save(SysInformation sysInformation,String[] projectIds) {
		sysInformationDao.save(sysInformation);
		writeIntoProjectInformationMapping(sysInformation,projectIds);
		return null;
	}

	@Override
	public void remove(String[] ids) {
		// TODO Auto-generated method stub
		for (int i = 0; i < ids.length; i++) {
			SysInformation sysInformation =	sysInformationDao.getSysInformationById(ids[i]);
			sysInformationDao.remove(sysInformation);
		}
		
	}

	/* (non-Javadoc)
	 * @see sy.service.SysInformationServiceI#update(sy.model.SysInformation)
	 */
	@Override
	public void update(SysInformation sysInformation,String[] projectIds) {
		// TODO Auto-generated method stub
		sysInformationDao.update(sysInformation);
		/**
		 * 先删除该公告信息下的项目，再插入所选的项目
		 */
		sysProjectInformationMappingService.removeByInformation(sysInformation.getId());
		writeIntoProjectInformationMapping(sysInformation,projectIds);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysInformationServiceI#list()
	 */
	@Override
	public List<SysInformation> list() {
		// TODO Auto-generated method stub
		return sysInformationDao.list();
	}

	/* (non-Javadoc)
	 * @see sy.service.SysInformationServiceI#getSysInformationById(java.lang.String)
	 */
	@Override
	public SysInformation getSysInformationById(String id) {
		// TODO Auto-generated method stub
		return sysInformationDao.getSysInformationById(id);
	}

	/* (non-Javadoc)
	 * @see sy.service.SysInformationServiceI#listSysInformationByKey(java.lang.String)
	 */
	@Override
	public List<SysInformation> listSysInformationByKey(String key) {
		// TODO Auto-generated method stub
		return sysInformationDao.listSysInformationByKey(key);
	}



}
